package cn.org.eshow.dao.hibernate;

import cn.org.eshow.bean.query.ProductCategoryQuery;
import cn.org.eshow.common.dao.EnhancedRule;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.ProductCategoryDao;
import cn.org.eshow.model.ProductCategory;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("productCategoryDao")
public class ProductCategoryDaoHibernate extends GenericDaoHibernate<ProductCategory, Integer> implements ProductCategoryDao {

    public ProductCategoryDaoHibernate() {
        super(ProductCategory.class);
    }

    public List<ProductCategory> list(ProductCategoryQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return list(rule);
    }

    public Page<ProductCategory> search(ProductCategoryQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        if (query != null) {
            rule.setOffset(query.getOffset());
            rule.setPageSize(query.pagesize);
        }
        return page(rule);
    }

    @Override
    public long count(ProductCategoryQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return count(ProductCategory.class, rule);
    }

    public EnhancedRule setRule(EnhancedRule rule, ProductCategoryQuery query) {

        if (query != null) {
            if (query.startAddTime != null) {
                rule.add(Restrictions.ge("addTime", query.startAddTime));
            }
            if (query.endAddTime != null) {
                rule.add(Restrictions.le("addTime", query.endAddTime));
            }
            if (query.startUpdateTime != null) {
                rule.add(Restrictions.ge("updateTime", query.startUpdateTime));
            }
            if (query.endUpdateTime != null) {
                rule.add(Restrictions.le("updateTime", query.endUpdateTime));
            }
            if (query.name != null) {
                rule.add(Restrictions.like("name", query.name, MatchMode.ANYWHERE));
            }
            if (query.description != null) {
                rule.add(Restrictions.like("description", query.description, MatchMode.ANYWHERE));
            }
            if (query.img != null) {
                rule.add(Restrictions.like("img", query.img, MatchMode.ANYWHERE));
            }
            if (query.sequence != null) {
                rule.add(Restrictions.eq("sequence", query.sequence));
            }
            if (query.enabled != null) {
                rule.add(Restrictions.eq("enabled", query.enabled));
            }
            if (query.parentId != null) {
                rule.add(Restrictions.eq("parent.id", query.parentId));
            }
            if (query.ifFirst != null) {
                if (query.ifFirst) {
                    rule.add(Restrictions.isNull("parent.id"));
                } else {
                    rule.add(Restrictions.isNotNull("parent.id"));
                }
            }
            if (!StringUtils.isEmpty(query.getOrder())) {
                rule.addOrder(query.getDesc() ? Order.desc(query.getOrder()) : Order.asc(query.getOrder()));
            }
        }
        return rule;
    }
}
